﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Configuration;
using FbKPI.Facade;
using FbKPI.Model;
using System.Web.UI;

namespace FBKPI
{
    public class KPISendEmail : System.Web.UI.Page
    {
        private string smtp = ConfigurationManager.AppSettings["smtp"].ToString();
        private int smtp_port = Convert.ToInt16(ConfigurationManager.AppSettings["PortSmtp"].ToString());
        private string SenderEmail = ConfigurationManager.AppSettings["SenderEmail"].ToString();
        private string SenderPassword = ConfigurationManager.AppSettings["SenderPassword"].ToString();
        private string SubjectEmail = ConfigurationManager.AppSettings["SubjectEmail"].ToString();
        private string BodyEmail = ConfigurationManager.AppSettings["BodyEmail"].ToString();
        private string SubjectEmailAchievment = ConfigurationManager.AppSettings["SubjectEmailAchievment"].ToString();
        private string BodyEmailAchievment = ConfigurationManager.AppSettings["BodyEmailAchievment"].ToString();


        public void KPIEmail(int KpiId, string EmployeeAssignBy,int EmployeeIDAssign)
        {
            KPI objKpi = new KPIFacade().GetKPIByID(KpiId);
            Employee objEmp = new EmployeeFacade().GetEmployeeByID(EmployeeIDAssign);
            if (objKpi != null)
            {
                string path = Server.MapPath("~/")+BodyEmail;
                BodyEmail = System.IO.File.ReadAllText(path).Replace("$NameAssign$",objEmp.FirstName).Replace("$NameAssignBy$",EmployeeAssignBy).Replace("$KpiDescription$",objKpi.Description).Replace("$Periode$",objKpi.Periode).Replace("$Weight$",objKpi.Weight.ToString()).Replace("$UomMaximum$",objKpi.UoMDetailMax.Description).Replace("$UomMinimum$",objKpi.UoMDetailMin.Description).Replace("$UoM$",objKpi.UoM.Description).Replace("$Year$",objKpi.Year.ToString());

                SendingEmail(BodyEmail, objEmp.Email,SubjectEmail);
            }
        }

        public void KPIEmailAchievment(int KpiId, string EmployeeAssignBy)
        {
            KPI objKpi = new KPIFacade().GetKPIByID(KpiId);
            int EmployeeIDAssign = objKpi == null ? 0 : objKpi.EmployeeIDVerifier;
            Employee objEmp = new EmployeeFacade().GetEmployeeByID(EmployeeIDAssign);
            if (objKpi != null)
            {
                string path = Server.MapPath("~/") + BodyEmailAchievment;
                BodyEmail = System.IO.File.ReadAllText(path).Replace("$NameAssign$", objEmp.FirstName).Replace("$NameAssignBy$", EmployeeAssignBy).Replace("$KpiDescription$", objKpi.Description).Replace("$Periode$", objKpi.Periode).Replace("$Weight$", objKpi.Weight.ToString()).Replace("$UomMaximum$", objKpi.UoMDetailMax.Description).Replace("$UomMinimum$", objKpi.UoMDetailMin.Description).Replace("$UoM$", objKpi.UoM.Description).Replace("$Year$", objKpi.Year.ToString());

                SendingEmail(BodyEmail, objEmp.Email,SubjectEmailAchievment);
            }
        }

        private void SendingEmail(string BodyEmail, string EmailTo, string SubjectEmail)
        {
            //BodyEmail = System.IO.File.ReadAllText(BodyEmail);

            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient(smtp);

            mail.From = new MailAddress(SenderEmail);
            mail.To.Add(EmailTo);

            mail.Subject = SubjectEmail;
            mail.Body = BodyEmail;
            mail.IsBodyHtml = true;

            SmtpServer.Port = smtp_port;
            SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
            SmtpServer.UseDefaultCredentials = false;
            SmtpServer.EnableSsl = false;
            SmtpServer.Credentials = new System.Net.NetworkCredential(SenderEmail, SenderPassword);
            SmtpServer.Send(mail);


        }
    }
}